package org.wlw.dao.impl;

import org.wlw.dao.IUserDao;
import org.wlw.models.User;
import org.wlw.models.UserException;
import org.wlw.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements IUserDao {
    @Override
    public void add(User user) {
        if(this.loadByUsername(user.getUsername())!=null) {
            throw new UserException("添加的用户已经存在!");
        }
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = DBUtil.getConnection();
            String sql = "insert into t_user(username,nickname,age,password) value (?,?,?,?)";
            ps = con.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getPassword());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(ps);
            DBUtil.close(con);
        }
    }

    @Override
    public void update(User user) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = DBUtil.getConnection();
            String sql = "update t_user set nickname=?,password=?,age=? where username=?";
            ps = con.prepareStatement(sql);
            ps.setString(1,user.getNickname());
            ps.setString(2,user.getPassword());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(ps);
            DBUtil.close(con);
        }
    }

    @Override
    public void delete(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = DBUtil.getConnection();
            String sql = "delete from t_user where id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(ps);
            DBUtil.close(con);
        }
    }

    @Override
    public void deleteByUsername(String username) {
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = DBUtil.getConnection();
            String sql = "delete from t_user where username=?";
            ps = con.prepareStatement(sql);
            ps.setString(1,username);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(ps);
            DBUtil.close(con);
        }
    }

    @Override
    public User load(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DBUtil.getConnection();
            String sql = "select * from t_user where id=?";
            ps = con.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            User user = null;
            while(rs.next()) {
                user = new User();
                user.setNickname(rs.getString("nickname"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setAge(rs.getInt("age"));
                user.setId(rs.getInt("id"));
            }
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(rs);
            DBUtil.close(ps);
            DBUtil.close(con);
        }
        return null;
    }

    @Override
    public User loadByUsername(String username) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DBUtil.getConnection();
            String sql = "select * from t_user where username=?";
            ps = con.prepareStatement(sql);
            ps.setString(1,username);
            rs = ps.executeQuery();
            User user = null;
            while(rs.next()) {
                user = new User();
                user.setUsername(rs.getString("username"));
                user.setAge(rs.getInt("age"));
                user.setNickname(rs.getString("nickname"));
                user.setPassword(rs.getString("password"));
                user.setId(rs.getInt("id"));
            }
            return user;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(rs);
            DBUtil.close(ps);
            DBUtil.close(con);
        }
        return null;
    }

    @Override
    public List<User> find() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        List<User> users = new ArrayList<>();
        try {
            con = DBUtil.getConnection();
            String sql = "select * from t_user";
            ps = con.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()) {
                User user = new User();
                user.setNickname(rs.getString("nickname"));
                user.setPassword(rs.getString("password"));
                user.setAge(rs.getInt("age"));
                user.setUsername(rs.getString("username"));
                user.setId(rs.getInt("id"));
                users.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtil.close(rs);
            DBUtil.close(ps);
            DBUtil.close(con);
        }
        return users;
    }

    @Override
    public User login(String username, String password) {
        User u = loadByUsername(username);
        if(u==null) {
            throw new UserException("用户名不存在!");
        }
        if(!password.equals(u.getPassword())) {
            throw new UserException("密码不正确!");
        }
        return u;
    }
}
